/**
 * AI算法
 * 
 */
var AIRules = function() {
	//规则权重
	this._weight = { FLUSH_WAY:1, FOUR_OF_A_KIND:2, FULL_HOUSE:3, FLUSH:4, WAY:5, THREE_OF_A_KIND:6, TWO_TWAIN:7, TWAIN:8 };	
		
	//取得公共牌可能组成的最大牌
	AIRules.prototype.getFormatPoker = function(r_u_poker, r_p_poker) {
		var r_cards;
		if (r_p_poker != null) {
			for each(var _poker in r_u_poker) {
				r_p_poker.push(_poker);
			}
			r_cards = r_p_poker;
		}else {
			r_cards = r_u_poker;
		}
		var _cards = this.sort(r_cards, 'DESC');
		var _result = new Object();
		_result['source'] 		 = _cards;
		_result['a2345'] 		 = { 'spade':Array(), 'heart':Array(), 'club':Array(), 'diamond':Array(), 'way':Array() };
		_result['flushWay'] 	 = { 'spade':Array(), 'heart':Array(), 'club':Array(), 'diamond':Array() };
		_result['fourOfAKind'] 	 = Array();
		_result['fullHouse'] 	 = Array();
		_result['flush'] 		 = { 'spade':Array(), 'heart':Array(), 'club':Array(), 'diamond':Array() };
		_result['way'] 		     = Array();
		_result['threeOfAKind']  = Array();
		_result['twoTwain'] 	 = Array();
		_result['twain'] 		 = Array();
		_result['offsuit']  	 = Array();
		var _amount = Array();
		var _way = Array();
		var _flush_way = {'spade':Array(), 'heart':Array(), 'club':Array(), 'diamond':Array() };
		var _i = 0;
		var _up_card ;
		var _label;
		var _value;
		var _suit ;
		trace("AIRules	getMaxPoker");
		for each(var _card in _result['source'])
		{
			_label = _card['label'];
			_value = _card['value'];
			_suit  = String(_card['suit']).toLocaleLowerCase();					
			_result['flush'][_suit].push(_card);
			var _tmp_amount =_amount[_value] == undefined ? 0: Number(_amount[_value]);
			_amount[_value] = _tmp_amount+1;
	
			if (1 == _amount[_value]) {
				_result['offsuit'].push(_card); 
			}else if (2 == _amount[_value]) {
				_up_card = _result['offsuit'].pop(); 
				_result['twain'].push(_up_card);
				_result['twain'].push(_card);
			}else if (3 == _amount[_value]) {
				_up_card = _result['twain'].splice( -2);
				_result['threeOfAKind'] = _result['threeOfAKind'].concat(_up_card);
				_result['threeOfAKind'].push(_card);
			}else if (4 == _amount[_value]) {
				_up_card = _result['threeOfAKind'].splice( -3);
				_result['fourOfAKind'] = _result['fourOfAKind'].concat(_up_card);
				_result['fourOfAKind'].push(_card);				
			}
			//判斷A 2 3 4 5
			if (14 == _value || (_value >= 2 && _value <= 5)) {
				_result['a2345'][_suit].push(_card);
				_result['a2345']['way'][_value] = _card;
			}
			//順子
			if ( 0 == _result['way'].length) {
				_result['way'].push(_card);
			}else if (_way['value'] - _value == 1) {
				_result['way'].push(_card);
			}else if (_way['value'] != _value) {				
				if (_result['way'].length < 5) {
					_result['way'] = Array();  
				}
				if (_i <= 2) {  //清空的牌數沒有超過3張,剩余的牌還有可能組成順子
					_result['way'].push(_card);
				}
			}
			//同花順
			if (0 == _flush_way[_suit].length) {
				_result['flushWay'][_suit].push(_card);
			}else if (_flush_way[_suit]['value'] - _value == 1) {
				_result['flushWay'][_suit].push(_card);
			}else {
				if (_result['flushWay'][_flush_way[_suit]['suit'].toLocaleLowerCase()].length < 5) {
					_result['flushWay'][_flush_way[_suit]['suit'].toLocaleLowerCase()].pop();
				}
				if(_i <= 2){
					_result['flushWay'][_suit].push(_card);
				}
			}
			_way = _card;
			_flush_way[_suit] = _card;
			_i++;			
		}	
		//处理三条加一对
		var _three_count = _result['threeOfAKind'].length;
		var _twain_count  = _result['twain'].length;
		if (_three_count > 0) {
			if (6 == _three_count) {
				var tmp_arr = _result['threeOfAKind'].splice(0, 5);
				_result['fullHouse'] = _result['fullHouse'].concat(tmp_arr); 
			}else if (_twain_count > 0) {
				_result['fullHouse'] = _result['threeOfAKind'];
				_result['fullHouse'] = _result['fullHouse'].concat(_result['twain'].splice(0, 2));
			}
			
		}
		// 处理两对
		if (_three_count == 0 && _twain_count >= 4 ) {
			_result['twoTwain'] = _result['twoTwain'].concat(_result['twain'].splice(0, 4));
		}		
		trace("AIRules	getMaxPoker");
		return _result;		
	}
	//取得电脑可能的最大牌	
	AIRules.prototype.getMaxRules = function(r_analyse_result) {
		var _my_max = { };
		_my_max['FLUSH_WAY'] = 0;
		_my_max['FOUR_OF_A_KIND'] = 0;
		_my_max['FULL_HOUSE'] = 0;
		_my_max['FLUSH'] = 0;
		_my_max['FOUR_OF_A_KIND'] = 0;
		_my_max['WAY'] = 0;
		_my_max['THREE_OF_A_KIND'] = 0;
		_my_max['TWO_TWAIN'] = 0;
		_my_max['TWAIN'] = 0;
		//同花順
		for (var _key in r_analyse_result['flushWay']) {
			if (r_analyse_result['flushWay'][_key].length > _my_max['FLUSH_WAY']) {				
				_my_max['FLUSH_WAY'] = r_analyse_result['flushWay'][_key].length;				
			}
		}
		//A2345同花順
		for each(var _key in r_analyse_result['a2345']) {
			if ('way' == _key) continue;
			if (r_analyse_result['a2345'].length > _my_max['FLUSH_WAY']) {
				_my_max['FLUSH_WAY'] = r_analyse_result['a2345'].length;				
			}
		}
		//四條
		if ( 4 == r_analyse_result['fourOfAKind']) {
			_my_max['FOUR_OF_A_KIND'] = 1;
		}
		//三條加一對	
		if ( 5 == r_analyse_result['fullHouse'].length) {
			_my_max['FULL_HOUSE'] = 1;			
		}		
		//同花
		for ( var _key in r_analyse_result['flush']) {			
			if (r_analyse_result['flush'][_key].length > _my_max['FLUSH']) {				
				_my_max['FLUSH'] = r_analyse_result['flush'][_key].length;				
			}
		}
		//順子			
		if (r_analyse_result['way'].length > _my_max['WAY'] ) {			
			_my_max['WAY'] = r_analyse_result['way'].length;			
		}
		//A2345 順子
		var _way_count = 0;
		var _way_cards = Array();
		for each(var _val in r_analyse_result['a2345']['way']) {
			_way_count++;
			_way_cards.push(_val);
		}
		if (_way_count > _my_max['WAY']) {
			_my_max['WAY'] = _way_count;
		}
		//三條
		var _count = r_analyse_result['threeOfAKind'].length;
		if (_count > 0) {
			_my_max['THREE_OF_A_KIND'] = 1;
		}
		//兩對
		if (4 == r_analyse_result['twoTwain'].length) {
			_my_max['TWO_TWAIN'] = 1;
		}
		//一對		
		if (2 == r_analyse_result['twain'].length) {
			_my_max['TWAIN'] = 1
		}
		return _my_max;
	}

	/**
	 * 数组排序
	 */
	AIRules.prototype.sort = function(r_cards, r_mode) {
		if (!r_cards) return;
		var _cards_sort = Array();
		if('DESC' == r_mode){ 
			r_cards.sort(_descOrder);
		}else {
			r_cards.sort(_ascOrder);
		}		
		return r_cards;
	}
	/**
	 * 设置各叫牌事件的概率与倍率
	 * @param	r_data	叫牌事件对象
	 * @param	r_lv	叫牌事件概率
	 * @param	r_bv	叫牌事件倍率
	 * @return	_data	叫牌对象
	 */
	AIRules.prototype.setAction = function(r_data,r_lv,r_bv) {
		var _data = { };
		if (r_data == null) {
			_data = { lv:r_lv, bv:r_bv };
		}else {
			_data['lv'] = (r_data['lv'] < r_lv ) ? r_lv : r_data['lv'];
			_data['bv'] = (r_data['bv'] < r_bv ) ? r_bv : r_data['bv'];
		}
		return _data;
	}
	/**
	 * 计算阶乘
	 */
	AIRules.prototype.factorial = function(r_a,r_b)  {
		r_b = Math.floor(r_b);  // 如果这个数不是一个整数，则向下舍入。
		if (r_b < 0)  {  // 如果这个数小于 0，拒绝接收。
			return -1;
		}
		if (r_b == 0)  {  // 如果为 0，则其阶乘为 1。
			return 1;
		}
		else return (r_a * this.factorial(r_a,(r_b - 1)));  // 否则，递归直至完成。
	}
}